$(function () {
    getProductData();
    searchFn();

    $("#more-sort-show").tap(function(){
        $(this).toggleClass("on");
        $("#more-sort").toggle();
    })
    $("#more-sort").on("tap","li",function(){
        $(this).addClass("on").siblings().removeClass("on");
        $("#more-sort").hide();
    })
    $('#orderBysales').tap(orderBysales);
    $("#zh").tap(getProductData);
    $("#sl").tap(orderBysales);
    $("#jg").tap(orderByPrice);
    $("#pl").tap(orderByComment);
    $('#proList').on('tap', '.title', linkFn).on('tap', '.buy', buyFn);


    function buyFn() {
        var id = $(this).attr('data-id');
        window.location.href = 'confirm_order.html?id=' + id;
    }

    function linkFn() {
        var id = $(this).attr('data-id');
        window.location.href = 'pro_details.html?id=' + id;
    }

    function orderByComment() {
        var option = {
            url: Api.getProductUrl,
            data: {
                getType: 'comment',
                num: 8
            }
        };
        requestEvent(option).then(function (data) {
            createProductHtml(data);
        })
    }

    function orderByPrice() {
        var option = {
            url: Api.getProductUrl,
            data: {
                getType: 'price',
                num: 8
            }
        };
        requestEvent(option).then(function (data) {
            createProductHtml(data);
        })
    }

    function orderBysales() {
        var option = {
            url: Api.getProductUrl,
            data: {
                getType: 'sales',
                num: 8
            }
        };
        requestEvent(option).then(function (data) {
            createProductHtml(data);
        })
    }

    function getParams() {
        var params = window.location.search;
        var obj = {};
        if (params) {
            params = params.replace('?', '').split('&');
            for (var i = 0, n = params.length; i < n; i++) {
                var key = params[i].split('=')[0];
                var val = params[i].split('=')[1];
                obj[key] = decodeURIComponent(val);
            }
        }
        return obj;
    }

    function searchFn() {
        var params = getParams();
        var search = params.search;
        if (!search) {
            return false;
        }
        var option = {
            url: Api.getProductUrl,
            data: {
                getType: 'search',
                num: 8,
                search: search
            }
        };
        requestEvent(option).then(function (data) {
            createProductHtml(data)
        })
        var searchHis = LsyStorage.getItem("searchHistory");
        searchHis = searchHis ? searchHis : {};
        searchHis[search] = search;
        LsyStorage.setItem("searchHistory",searchHis);
    }

    function createProductHtml(data) {
        var str = '';
        data.forEach(function (item) {
            str += '<div class="sum">'
                + '<div class="sum_left">'
                + '<a href="pro_details.html?id=' + item.id + '"><img src="' + item.img_src + '" alt="zhong1"></a>'
                + '</div>'
                + '<div class="sum_right">'
                + '<h3 class="title" data-id="' + item.id + '">' + item.title + " " + item.subtitle + '</h3>'
                + '<h4><i>破损包退</i><b>包邮</b></h4>'
                + '<h5>￥<span>' + item.price + '</span></h5>'
                + '<button class="buy" data-id="' + item.id + '">立即购买</button>'
                + '</div>'
                + '</div>';
        })
        $('#proList').html(str);
    }

    function getProductData() {
        var option = {
            url: Api.getProductUrl,
            data: {
                getType: 'default',
                num: 8
            }
        };
        requestEvent(option).then(function (data) {
            createProductHtml(data)
        })
    }

});